package suites.deeplink {
	import org.splink.deepsplink.navigation.INavigation;

	import suites.stub.NavigationStub;

	import org.flexunit.asserts.assertEquals;
	import org.splink.deepsplink.navigation.command.INavigationCommand;
	import org.splink.deepsplink.navigation.command.NavigationCommand;

	/**
	 * @author Max Kugland
	 */
	public class NavigationCmdTest extends AbstractDeeplinkControllerTest {

		[Test]
		public function basic() : void {
			createCtrl().start();
			var cmd : INavigationCommand = new NavigationCommand(_ctrl);
			cmd.gotoPage("2");			cmd.gotoPage("3");			cmd.gotoPage("4");
			
			assertEquals(4, _ctrl.history.length);
		}

		[Test]
		public function register() : void {
			createCtrl();
			var cmd : INavigationCommand = new NavigationCommand(_ctrl);
			var a : Array = new Array(); 
			cmd.registerNavigation(new NavigationStub(function(id : String, params : Array, changed : Boolean) : void {
				a.push(id);
			}));
			
			cmd.gotoPage("4");
			_ctrl.start();
			assertEquals("4", a[0]);
			assertEquals(1, _ctrl.history.length);
		}
		
		[Test]
		public function unregister() : void {
			createCtrl();
			var cmd : INavigationCommand = new NavigationCommand(_ctrl);
			var a : Array = new Array();
			var mock : INavigation = new NavigationStub(function(id : String, params : Array, changed : Boolean) : void {
				a.push(id);
			}); 
			cmd.registerNavigation(mock);			cmd.gotoPage("4");			_ctrl.start();
			cmd.unregisterNavigation(mock);
			cmd.gotoPage("3");
			assertEquals("4", a[0]);			assertEquals(1, a.length);
			assertEquals(2, _ctrl.history.length);
		}
		
		[Test]
		public function back() : void {
			createCtrl();
			var cmd : INavigationCommand = new NavigationCommand(_ctrl);
			
			assertEquals(0, _ctrl.history.length);			cmd.back();
			assertEquals(0, _ctrl.history.length);
			
			_ctrl.start();
			cmd.gotoPage("1");			cmd.gotoPage("2");			cmd.gotoPage("3");			cmd.gotoPage("4");
			
			cmd.back();
			assertEquals("3", _ctrl.history[_ctrl.history.length-1]);
			cmd.back();
			assertEquals("2", _ctrl.history[_ctrl.history.length-1]);
			cmd.back();
			assertEquals("1", _ctrl.history[_ctrl.history.length-1]);
			cmd.back();
			assertEquals("1", _ctrl.history[_ctrl.history.length-1]);
			cmd.back();
			cmd.back();
			cmd.back();
			assertEquals("1", _ctrl.history[_ctrl.history.length-1]);
		}
	}
}
